package com.neu.servlet.ww;

import com.neu.service.ww.GoodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DelCarServlet",urlPatterns = "/DelCarServlet.do")
public class DelCarServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();//1234 3
        //session.invalidate();

//        List<Map<String,Object>> carList= (List<Map<String, Object>>) session.getAttribute("carList");
//        List<List<String>> lists=new ArrayList<>();
//        List<String> list=new ArrayList<>();
//        for (int i = 0; i < carList.size(); i++) {
//            list.add(carList.get(i).get("num").toString());
////            System.out.println(carList.get(i).get("NUM").toString());
//            list.add(carList.get(i).get("G_ID").toString());
//            lists.add(list);
//        }
//        GoodsService goodsService=new GoodsService();
//        int row=goodsService.doUpdateAllNumById(lists);
//        session.removeAttribute("carList");
//        List<Map<String,Object>> car=new ArrayList<>();
//
//        System.out.println("------------------"+row);
//        if (row>0){
//            session.setAttribute("carList",car);
//        }


        List<Map<String,Object>> carList= (List<Map<String, Object>>) session.getAttribute("carList");
        List<List<String>> lists=new ArrayList<>();
        List<String> list=new ArrayList<>();
        for (int i = 0; i < carList.size(); i++) {
            list.add(carList.get(i).get("num").toString());
//            System.out.println(carList.get(i).get("NUM").toString());
            list.add(carList.get(i).get("G_ID").toString());
            lists.add(list);
        }
        GoodsService goodsService=new GoodsService();
        int row=goodsService.doUpdateAllNumById(lists);
        System.out.println(row);
        if (row>0){
            carList=new ArrayList<>();
            session.setAttribute("carList",carList);
            request.getRequestDispatcher("/WEB-INF/views/car.jsp").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
